import getApiData, {
  delProjectInit,
  delZcbfInit,
  getApisData,
  getNestApiData,
} from '@/services/cpasRequest';

export async function getUpdateZcbf(paramsData: any, dataWhere: any) {
  const tempUser = [
    {
      ...paramsData,
      ...{
        rowState: 'update',
        where: dataWhere,
      },
    },
  ];

  const postData = {
    apis: [
      {
        name: 'sqlApi.update',
        params: { name: 'sjxm_zcbf', rows: tempUser },
      },
    ],
    reload: true,
    params: {},
  };
  const res: any = await getApisData(postData);
  const resTemp = res.data['sqlApi.update'];
  if (res.ok && resTemp.ok) {
    return Promise.resolve({
      data: 'sjxm_zcbf, 修改成功！',
      success: true,
    });
  }
  return Promise.resolve({
    data: res.data,
    success: false,
  });
}

export async function delZcbfInitRequest(params: { zcbfid: string }) {
  try {
    const res: any = await delZcbfInit(params);
    if (res.ok) {
      return Promise.resolve({
        data: '组成部分删除成功！',
        success: true,
      });
    }
    return Promise.resolve({
      data: '组成部分删除失败！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: 'catch,报错了',
      success: false,
    });
  }
}

export async function delProjectRequest(params: { projectBh: string }) {
  try {
    const res: any = await delProjectInit(params);
    if (res.ok) {
      return Promise.resolve({
        data: '项目删除成功！',
        success: true,
      });
    }
    return Promise.resolve({
      data: '项目删除失败！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: 'catch,报错了',
      success: false,
    });
  }
}

// 删除本地项目和组成部分
export async function deleteLocalItem(params: any) {
  try {
    const res = await getApiData('pm.deleteLocalItem', params);
    if (res.code === 0) {
      return Promise.resolve({
        data: '项目和组成部分，删除成功！',
        success: true,
      });
    }
    return Promise.resolve({
      data: '项目和组成部分，删除失败！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: 'catch,报错了',
      success: false,
    });
  }
}

export async function getPeriodInfo(params: { dbname: string; reportType?: string }) {
  try {
    const res = await getApiData('account.getPeriodInfo', params);
    if (res.ok) {
      let settingCol: any = {
        dbname: params.dbname,
        yearCnt: res.data.yearCnt,
        minData_id: res.data.minData_id,
      };
      if (params.reportType) {
        settingCol = { ...settingCol, bgType: params.reportType };
      }
      const resCol = await getApiData('account.calcReportSettingColumn', settingCol);
      if (resCol.ok) {
        return Promise.resolve({
          data: res.data,
          success: true,
        });
      }
      return Promise.resolve({
        data: res.data,
        success: false,
      });
    }
    return Promise.resolve({
      data: '接口account.getPeriodInfo出错了！',
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口account.getPeriodInfo出错了！',
      success: false,
    });
  }
}

// audit 初始化模板
export async function templateInitApi(params: {
  dbname: string;
  templateName: string;
  tableNames: string[];
  type: string;
  dateId?: string;
}) {
  try {
    const res = await getNestApiData('/template/init', 'post', params);
    if (res.ok) {
      return Promise.resolve({
        data: res.ok,
        msg: res.msg,
        success: true,
      });
    }
    return Promise.resolve({
      data: res.ok,
      msg: res.msg,
      success: false,
    });
  } catch (error) {
    return Promise.resolve({
      data: '接口/template/init,请求失败！',
      success: false,
    });
  }
}
